package restful.servlet;

import static restful.Methods.*;

import java.util.*;

import javax.servlet.http.*;

import restful.*;


public class RestfulResolver {

	private final RestfulProtocolMatcher mather;

	public RestfulResolver(final RestfulProtocolMatcher mather) {
		this.mather = mather;
	}

	private String pseudoMethodOf(final HttpServletRequest request) {
		final String httpMethod = request.getMethod();
		final String _METHOD = request.getParameter("_METHOD");
		return new OverrideHttpMethod(httpMethod, _METHOD).toString();
	}

	public RestfulMethod resolve(final HttpServletRequest request) {
		final String uri = new Formatter().format("%s %s%s", pseudoMethodOf(request), request.getRequestURI(), methodOf(request.getQueryString())).toString();
		return mather.method(uri);
	}
}
